package com.loocan.answer.utils;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.Build;

import androidx.annotation.RequiresApi;

import android.content.Context;
import android.media.MediaPlayer;

/**
 * @author liupeng QQ:806875201
 * @time 2025/6/23 22:41
 * @Description describe
 */
public class SoundPlayer {
    private static MediaPlayer mediaPlayer;
    private static boolean isLooping = false;
    private static int repeatCount = 0;
    private static int currentPlayCount = 0;

    // 初始化声音播放器
    public static void initialize(Context context) {
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
        }
    }

    // 加载声音文件（单次播放）
    public static void loadSound(Context context, int soundId) {
        loadSound(context, soundId, false, 0);
    }

    // 加载声音文件（带循环设置）
    public static void loadSound(Context context, int soundId, boolean looping) {
        loadSound(context, soundId, looping, looping ? -1 : 0);
    }

    // 加载声音文件（带重复次数设置）
    public static void loadSound(Context context, int soundId, boolean looping, int repeatTimes) {
        stopSound();

        try {
            mediaPlayer = MediaPlayer.create(context, soundId);
            isLooping = looping;
            repeatCount = repeatTimes;

            if (looping && repeatTimes == -1) {
                mediaPlayer.setLooping(true);
            } else {
                mediaPlayer.setLooping(false);
                mediaPlayer.setOnCompletionListener(mp -> handleCompletion());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 处理播放完成事件
    private static void handleCompletion() {
        if (!isLooping) {
            if (repeatCount > 0 && currentPlayCount < repeatCount) {
                currentPlayCount++;
                mediaPlayer.start();
            } else {
                currentPlayCount = 0;
                mediaPlayer.setOnCompletionListener(null);
            }
        }
    }

    // 播放声音
    public static void playSound() {
        if (mediaPlayer != null && !mediaPlayer.isPlaying()) {
            try {
                mediaPlayer.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    // 暂停声音
    public static void pauseSound() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
        }
    }

    // 停止声音
    public static void stopSound() {
        if (mediaPlayer != null) {
            try {
                mediaPlayer.reset();
                mediaPlayer.release();
                mediaPlayer = null;
                currentPlayCount = 0;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    // 设置循环播放
    public static void setLooping(boolean looping) {
        isLooping = looping;
        if (mediaPlayer != null) {
            if (looping) {
                mediaPlayer.setLooping(true);
                repeatCount = -1;
            } else {
                mediaPlayer.setLooping(false);
                mediaPlayer.setOnCompletionListener(mp -> handleCompletion());
            }
        }
    }

    // 设置重复次数
    public static void setRepeatCount(int count) {
        repeatCount = count;
        if (mediaPlayer != null) {
            mediaPlayer.setLooping(false);
            isLooping = false;
            mediaPlayer.setOnCompletionListener(mp -> handleCompletion());
        }
    }

    // 检查是否正在播放
    public static boolean isPlaying() {
        return mediaPlayer != null && mediaPlayer.isPlaying();
    }

    // 获取当前播放次数
    public static int getCurrentPlayCount() {
        return currentPlayCount;
    }

    // 获取总重复次数
    public static int getRepeatCount() {
        return repeatCount;
    }
}

